package com.lilingguo.easyserver.container;

import com.lilingguo.easyserver.dao.ServiceInfoDao;
import com.lilingguo.easyserver.pojo.ServiceInfo;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * SQL容器
 *
 * @Author: 黎铃果
 * @Date: 2020/05/28 16:35
 */
public class SqlContainer implements Container {

    private final ServiceInfoDao serviceInfoDao;

    public SqlContainer(ServiceInfoDao serviceInfoDao) {
        this.serviceInfoDao = serviceInfoDao;
    }

    @Override
    public void init() {
        serviceInfoDao.init();
    }

    @Override
    public boolean saveInfo(ServiceInfo info) {
        if (serviceInfoDao.countInfoByIdStr(info) == 0) {
            return serviceInfoDao.saveInfo(info) == 1;
        }
        return false;
    }

    @Override
    public boolean removeInfo(ServiceInfo info) {
        return serviceInfoDao.removeInfo(info) == 1;
    }

    @Override
    public void removeExpiredInfos() {
        serviceInfoDao.removeExpiredInfos();
    }

    @Override
    public Set<ServiceInfo> getInfo(String appName) {
        List<ServiceInfo> list = serviceInfoDao.getInfos(appName);
        return new HashSet<ServiceInfo>(list);
    }

    @Override
    public boolean keepAlive(ServiceInfo info) {
        return serviceInfoDao.keepAlive(info) == 1;
    }

    @Override
    public Set<ServiceInfo> getAllData() {
        List<ServiceInfo> list = serviceInfoDao.getAllData();
        return new HashSet<ServiceInfo>(list);
    }
}
